/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.SwapInterface;
import br.gov.cemaden.library.model.smp.ExecHardware;
import br.gov.cemaden.library.model.smp.Swap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class SwapDao extends GenericDao implements SwapInterface {

    private Session session;

    public SwapDao(Session session) {
        this.session = session;
    }

    public List<Swap> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Swap> swap = null;
        try {
            Query query = session.getNamedQuery("Swap.findAll");
            swap = (List<Swap>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no SwapFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return swap;
    }

    public Swap findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Swap swap = null;
        try {
            Query query = session.getNamedQuery("Swap.findById").setParameter("id", id);
            swap = (Swap) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no SwapFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return swap;
    }

    public List<Swap> findBySwapUsed(Double swapUsed) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Swap> swap = null;
        try {
            Query query = session.getNamedQuery("Swap.findBySwapUsed").setParameter("swapUsed", swapUsed);
            swap = (List<Swap>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no SwapFacade.findBySwapUsed: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return swap;
    }

    public List<Swap> findBySwapTotal(Double swapTotal) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Swap> swap = null;
        try {
            Query query = session.getNamedQuery("Swap.findBySwapTotal").setParameter("swapTotal", swapTotal);
            swap = (List<Swap>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no SwapFacade.findBySwapTotal: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return swap;
    }

    public List<Swap> findBySwapFree(Double swapFree) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Swap> swap = null;
        try {
            Query query = session.getNamedQuery("Swap.findBySwapFree").setParameter("swapFree", swapFree);
            swap = (List<Swap>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no SwapFacade.findBySwapFree: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return swap;
    }

    public List<Swap> findByExecHardware(ExecHardware execHardware) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Swap> swap = null;
        try {
            Query query = session.getNamedQuery("Swap.findByExecHardware").setParameter("execHardware", execHardware);
            swap = (List<Swap>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no SwapFacade.findByExecHardware: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return swap;
    }

    public Swap insert(Swap swap) {
        try {
            saveOrUpdate(this.session, swap);
        } catch (Exception ex) {
            Logger.getLogger(SwapDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return swap;
    }

    public Swap update(Swap swap) {
        try {
            saveOrUpdate(this.session, swap);
        } catch (Exception ex) {
            Logger.getLogger(SwapDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return swap;
    }

    public void delete(Swap swap) {
        try {
            remove(this.session, swap);
        } catch (Exception ex) {
            Logger.getLogger(SwapDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
